import { Ref, onMounted, onUnmounted, ref } from 'vue'
import evaluateStore from '@/piniastore/evaluate'
import { storeToRefs } from 'pinia'
import { getOneItemValuesFrmArr, getOneitemValuesFrmArr } from '@/utils'

export default class BookDetailsService {
  static imgRef = ref<HTMLBodyElement | undefined>()
  static headerOpacity = ref({ opacity: 1 })
  static bookScroll() {
    const handleScroll = () => {
      console.log('handleScroll')
    }

    onMounted(() => {
      window.addEventListener('scroll', handleScroll)
    })

    onUnmounted(() => {
      window.removeEventListener('scroll', handleScroll)
    })

    const scrollTop = document.body.scrollTop || document.documentElement.scrollTop

    const picHeight = BookDetailsService.imgRef.value!.offsetHeight
    if (scrollTop > 90) {
      BookDetailsService.headerOpacity.value.opacity = scrollTop / picHeight
      console.log('scrollTop', scrollTop)
    } else {
      BookDetailsService.headerOpacity.value.opacity = 0
    }
  }
}

export class EvaluateClass {
  static store = evaluateStore()
  static storeRef = storeToRefs(EvaluateClass.store)
  static goodEvalNum = ref(0)
  static mediumEvalNum = ref(0)
  static negativeEvalNum = ref(0)
  static async searchEvalRplLst(isbn: string) {
    await EvaluateClass.store.findEvalRplLst(isbn)
    EvaluateClass.calEvalDegrees()
  }
  static restoreEvalNum() {
    EvaluateClass.goodEvalNum.value = 0
    EvaluateClass.mediumEvalNum.value = 0
    EvaluateClass.negativeEvalNum.value = 0
  }
  static calEvalDegrees() {
    EvaluateClass.restoreEvalNum()
    const evalDegrees = getOneItemValuesFrmArr(EvaluateClass.store.evalRplLst, 'evaluatedegree')
    evalDegrees.forEach((e) => {
      if (e === 1) {
        EvaluateClass.goodEvalNum.value++
      } else if (e === 2) {
        EvaluateClass.mediumEvalNum.value++
      } else if (e === 3) {
        EvaluateClass.negativeEvalNum.value++
      }
    })
  }
}
